<%@page import="com.office.automate.model.employee.Employee"%>
<%@page import="com.office.automate.service.impl.employee.EmployeeQueryServiceImplementer"%>
<%@page import="com.office.automate.service.def.employee.EmployeeQueryService"%>
<%@page import="java.util.List"%>
<%@page import="com.office.automate.model.administration.Branch"%>
<%@page import="com.office.automate.exception.framework.FrontierVisibleException"%>
<%@page import="com.office.automate.service.impl.administration.AdministrationServiceImplementer"%>
<%@page import="com.office.automate.service.def.administration.AdministrationService"%>
<%@page import="com.office.automate.configuration.property.DatagridPageConfiguration"%>
<%@page import="com.office.automate.frontier.utility.DataConversionUtility"%>
<%@page import="com.office.automate.configuration.property.ConfigurationProperties"%>
<%@page import="java.util.Properties"%>
<%@page import="com.office.automate.frontier.utility.ExceptionMessagesManagement"%>
<%@page import="com.office.automate.validation.framework.ValidatorObject"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@ include file="../common/CommonHeaderInfo.jsp"%>
<title>Manage Branches</title>
</head>
<body>
	<%@ include file="../common/SiteHeaderSection.jsp" %>
	<%@ include file="../common/AdministrationMenu.jsp"%>
		<%
			if (currentUser == null) {
				return;
			}
			Properties configSeqPropspg = ConfigurationProperties.getProps(); 
			Utility utilpg = new Utility(currentUser.getSecurityLevels());
			if (!utilpg.isFunctionalSecurityAllow(configSeqPropspg.getProperty("MAIN_ADMINISTRATION")) ||  
						!utilpg.isFunctionalSecurityAllow(configSeqPropspg.getProperty("MANAGE_BRANCHES")))  {
				response.sendRedirect("/OfficeAutomation/jsp/HomePage.jsp");
			}			
			Properties configProps = ConfigurationProperties.getProps();
		    DataConversionUtility dataconversion = new DataConversionUtility();
			String aDataGridPageID = DatagridPageConfiguration.BRANCH_DETAILS;
			String aNextDataRowNumber = (String)request.getAttribute("NextDataRowNumber");
			boolean isValidationErrorExist = false;
			ExceptionMessagesManagement exceptionMessagesManagement  = null;
			Branch branch = (Branch) request.getAttribute("Branch");
			ValidatorObject validationErrors = (ValidatorObject) request.getAttribute("FieldValidationErrors");
			AdministrationService administrationService = new AdministrationServiceImplementer();
			EmployeeQueryService employeeQueryService = new EmployeeQueryServiceImplementer();
			FrontierVisibleException operationalExcaption = (FrontierVisibleException) request.getAttribute("OperationalError");
			if (operationalExcaption != null) {
		%>
		<tr>
			<td>
				<table width="100%" class="operationfaliererror">
					<tr>
						<td>
							<center>
								<font class="operationfaliererrorfont"> <b> Opps.....!!! <%=operationalExcaption.getDetailsErrorMessage() %> </b> <br /> </font>
							</center>
						</td>
					</tr>
		
				</table>
			</td>
		</tr>
		<% } %>
		<tr>
			<td>
				<form action="/OfficeAutomation/ManageBranchesServlet" method="post">
					<center>
						<table>
							<tr>
							<%
							  if (validationErrors != null) {
									exceptionMessagesManagement = new ExceptionMessagesManagement(validationErrors.getValidationErrorList());
									isValidationErrorExist = true;
							  }
							 %>
								<td>
									<center><font class="formtablecaption"><u>Branch Declaration</u></font></center><br />
									<table class="mainContentStyles">
										<tr>
											<td>Branch Description</td>
											<td><input type="text" name="branchdescription" size="20" <% if (isValidationErrorExist) { %>value="<%=branch.getDescription() %>" <% } %>></td>
										</tr>
										<%
										 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("branchdescription")) {
										%>
											<tr>
												<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("branchdescription")  %> </font></td>
											</tr>
										<%
										 }
										%>
										<tr>
											<td>Branch Location</td>
											<td><input type="text" name="branchlocation" size="20" <% if (isValidationErrorExist) { %>value="<%=branch.getLocation() %>" <% } %>></td>
										</tr>
										<%
										 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("branchlocation")) {
										%>
											<tr>
												<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("branchlocation")  %> </font></td>
											</tr>
										<%
										 }
										%>
										<tr>
											<td>Manager / Supervisor</td>
											<td><select name="managerorsupervisor">
													<option />
													<%
													List<Employee> list = employeeQueryService.getManagersOrSupervisors();
														for (int i=0; i< list.size(); i++) {
															Employee employee = list.get(i);
													%>
														<option value="<%=employee.getEmployeeId() %>" <% if (isValidationErrorExist && employee.getEmployeeId() == branch.getManagerOrSupervisorId()) { %> selected="selected" <% } %>><%=employee.getFirstName() %></option>
													<%
														}
													%>
												</select>
											</td>
										</tr>
									    <%
										 if (isValidationErrorExist && exceptionMessagesManagement.checkExistenceOfErrorByKey("managerorsupervisor")) {
										%>
											<tr>
												<td colspan="2"><font class="errorMessageStyles"> <%=exceptionMessagesManagement.getDetailExceptionMessage("managerorsupervisor")  %> </font></td>
											</tr>
										<%
										 }
										%>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="2"><div class="formButtonPanel"><input type="reset" class="buttonstyle"> <input class="buttonstyle" type="submit" name="submit" value="Submit" /></div></td>
							</tr>
						</table>
					</center>
				 </form>
			</td>
		</tr>
		<tr>
			<td height="20px">
			
			</td>
		</tr>
		<tr>
			<td>
				<center>
				        <jsp:include page="../frameworks/DataGridComponent.jsp">
			            	<jsp:param name="aDataGridPageID" value="<%=aDataGridPageID %>" />
			            	<jsp:param name="aNextRowNumber" value="<%=aNextDataRowNumber %>" />
			            	<jsp:param name="aButtonDeleteCaption" value="Delete" />
			            	<jsp:param name="aButtonsNextPrevious" value="true" />
			        	</jsp:include>
				</center>
			</td>
		</tr>
<%@ include file="../common/SiteFotterSection.jsp" %>
</body>
</html>